home *** CD-ROM | disk | FTP | other *** search
- Path: unix.sri.com!usenet
- From: mklenk@updike.sri.com (Mark Klenk)
- Newsgroups: comp.lang.c
- Subject: Re: Examples of using "volatile"?
- Date: 18 Jan 1996 16:43:52 GMT
- Organization: SRI International
- Message-ID: <4dltc8$9ak@unix.sri.com>
- References: <4djoj2$mr1@post.gsfc.nasa.gov>
- Reply-To: mklenk@updike.sri.com
- NNTP-Posting-Host: 204.75.161.40
-
- Stephen Maher wrote:
- >
- >I'd like a concrete example(s) illustrating a reason to
- >use the "volatile" type qualifier.
-
- This is mostly useful for variables or memory locations
- which could be written to by interrupt handlers or other
- processors sharing the same memory space.
-
- Example: Two processors need to communicate via shared
- memory. One writes a number to a shared location,
- the other reads it (producer/consumer).
-
- The following implementation of the example is not the best,
- but its point is simply to show the use of the keyword.
-
- producer code:
-
- int main(void)
- {
- int volatile * pnum = (int *)0x0000ffff;
-
- for (i = 0; i < 20000; ++i) {
- *pnum = i;
- }
- return 0;
- }
-
- consumer code (running on a different processor):
-
- #include <stdio.h>
-
- int main(void)
- {
- int const volatile * pnum = (int const *)0x0000ffff;
-
- while (1) {
- printf("%d\n", *pnum);
- }
- return 0;
- }
-
- I hope this helps.
-
- ---
-
- mklenk@coronacorp.com (Mark Klenk)
-
-
-
-